var usedata = require("../paydata/paydata.js");
Page({
  data: {
    address: "",//地址
    count: "",
    countSum: "170.00",
    cashCouponText: "10.00",//代金券金额
    cashCouponText2: "10.00",//代金券金额
    cashCouponText3: "0",
    cashCouponText4: "10.00",//代金券金额
    cardTextNum: "12.00",//体验卡
    cardTextNum2: "0",//体验卡
    cardText: "0.00",
    hasList: true,
    selectAllStatus: true, // 全选状态，默认全选
    cardBtn: false,
    cardHidden: true,
    cardWrapHidden: true,
    productNumber: 3,
    shoppingNumber: "3",
    vouchers: "",//代金券
    giftPrice1: ""//满金额
  },
  staticData: {//验证码提交
  },
  onLoad: function (options) {
    wx.setTabBarBadge({//购物车数量
      index: 1,
      text: this.data.shoppingNumber
    })
    this.setData({
      count: usedata.setdata,
      address: usedata.address,
      vouchers: usedata.vouchers,
      giftPrice1: usedata.vouchers[0].giftPrice,
    })
  },
  add: function (e) {
    // 加
    var count = this.data.count;
    var index = e.currentTarget.dataset.index;
    count[index].num++;
    count[index].price = (count[index].num * count[index].danjia * count[index].day).toFixed(2);
    this.setData({
      count: count
    });

    this.countSum()
  },
  minus: function (e) {
    // 减
    var count = this.data.count;
    var index = e.currentTarget.dataset.index;
    if (count[index].num <= 1) {
      count[index].num = 1
    } else {
      count[index].num--;
    }
    count[index].price = (count[index].num * count[index].danjia * count[index].day).toFixed(2);
    this.setData({
      count: count
    })
    this.countSum()
  },
  remove: function (e) {
    // 删除
    var count = this.data.count;
    var that = this;
    wx.showModal({
      title: '提示',
      content: '确认删除吗？',
      success: function (res) {
        if (res.confirm) {
          // console.log('用户点击确定')
          // 获取要删除数据的id 
          var index = e.currentTarget.dataset.index;
          // 删除数组对应的数据内容 
          var release = count;
          count.splice(index, 1)
          //判断数据的长度 
          var len = count.length;
          //通过判断数组的长度来决定是否显示隐藏的部分 
          // console.log(count)
          that.setData({
            count: count
          });
          // 如果数据为空
          if (!count.length) {
            that.setData({
              hasList: false
            });
            that.countSum()
          } else {
            // 调用金额渲染数据
            that.countSum()
          }
        } else if (res.cancel) {
          console.log('用户点击取消删除')
        }
      }

    })
  },
  countSum: function () {
    // 总价
    var count = this.data.count;
    var countSum = 0;
    var productNumber = []
    for (var i = 0; i < count.length; i++) {
      // 判断选中计算价格
      if (count[i].checked) {
        //   // 所有价格加起来 count_money
        countSum += Number(count[i].price);
        productNumber.push(count[i])

      }
    }
    if (countSum > 0) {
      countSum = countSum - this.data.cashCouponText2 - this.data.cardText;
      if (countSum < 0) {
        countSum = 0
      }
    } else {
      countSum = countSum
    }
    this.setData({
      countSum: countSum.toFixed(2),
      productNumber: productNumber.length
    })

  },
  selectList(e) {
    // 当前商品选中事件
    var that = this;
    // 获取选中的radio索引
    var index = e.currentTarget.dataset.index;
    // 获取到商品列表数据
    var count = that.data.count;
    // 默认全选
    that.data.selectAllStatus = true;
    // 循环数组数据，判断----选中/未选中[checked]
    count[index].checked = !count[index].checked;
    // 如果数组数据全部为checked[true],全选
    for (var i = count.length - 1; i >= 0; i--) {
      if (!count[i].checked) {
        that.data.selectAllStatus = false;
        break;
      }
    }
    // 重新渲染数据
    that.setData({
      count: count,
      selectAllStatus: that.data.selectAllStatus
    })
    console.log(this.data.selectAllStatus)
    // 调用计算金额方法
    that.countSum();
  },
  selectAll(e) {
    // 购物车全选事件
    // 全选ICON默认选中
    let selectAllStatus = this.data.selectAllStatus;
    // true  -----   false
    selectAllStatus = !selectAllStatus;
    // 获取商品数据
    let count = this.data.count;
    // 循环遍历判断列表中的数据是否选中
    for (let i = 0; i < count.length; i++) {
      count[i].checked = selectAllStatus;
    }
    // 页面重新渲染
    this.setData({
      selectAllStatus: selectAllStatus,
      count: count
    });
    // 计算金额方法
    this.countSum();
  },
  cardWrapHidden() {//体验卡账号显示
    var cardWrapHidden = this.data.cardWrapHidden;
    cardWrapHidden = !this.data.cardWrapHidden;
    this.setData({
      cardWrapHidden: cardWrapHidden
    });
  },
  accountChange(e) {//账号监听
    this.staticData.accountChange = e.detail.value;
  },
  passwordtChange(e) {//账号监听
    this.staticData.passwordChange = e.detail.value;
  },
  cardSubmit: function () {//验证码提交
    if (!this.staticData.accountChange) {
      wx.showToast({
        title: '请输入账号',
        icon: 'loading',
        // image:'../images/details/1.png',
        duration: 500
      })
      return;
    }
    if (!this.staticData.passwordChange) {
      wx.showToast({
        title: '请输入密码',
        icon: 'loading',
        duration: 500
      })
      return;
    }
    this.setData({
      cardBtn: true,
      cardHidden: false,
      cardText: this.data.cardTextNum,
      cashCouponText2: this.data.cashCouponText3,
      cardWrapHidden: true
    });
    this.countSum()
  },
  changeSwitch: function () {//是否使用体验卡
    var changedData = this.data.cardBtn;
    changedData = !this.data.cardBtn;
    if (changedData) {
      this.setData({
        cardText: this.data.cardTextNum,
        cashCouponText2: this.data.cashCouponText3
      });
    } else {
      this.setData({
        cardText: this.data.cardTextNum2,
        cashCouponText2: this.data.cashCouponText
      });
    }
    this.setData({
      cardBtn: changedData
    });
    this.countSum()
  },
  vouchersRadio: function (e) {//选择代金券
    console.log('radio发生change事件，携带的value值为：', e.detail.value)
    if (!this.data.cardBtn) {
      this.setData({
        giftPrice1: e.detail.value,
        // cashCouponText: e.detail.value,
        cashCouponText4: e.detail.value,
        // cashCouponText2: e.detail.value,
      })
    } else {
      this.setData({
        giftPrice1: e.detail.value,
        // cashCouponText: e.detail.value,
        cashCouponText4: e.detail.value,
        // cashCouponText2: this.data.cashCouponText3
      })
    }
    // this.countSum()
  },
  giftSelectBtn: function () {//确认代金券金额
    if (!this.data.cardBtn) {
      this.setData({
        cashCouponText: this.data.cashCouponText4,
        cashCouponText2: this.data.cashCouponText4,
      })
    } else {
      this.setData({
        cashCouponText: this.data.cashCouponText4,
        cashCouponText2: this.data.cashCouponText3
      })
    }
    this.countSum();
    this.hideModal();
  },
  //点击我显示优惠券弹出框
  clickme1: function () {
    this.showModal();
  },
  //显示对话框
  showModal: function () {
    // 显示遮罩层
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation
    animation.translateY(300).step()
    this.setData({
      animationData: animation.export(),
      showModalStatus: true
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export()
      })
    }.bind(this), 200)
  },
  //隐藏对话框
  hideModal: function () {
    // 隐藏遮罩层
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation
    animation.translateY(300).step()
    this.setData({
      animationData: animation.export(),
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export(),
        showModalStatus: false
      })
    }.bind(this), 200)
    this.setData({
      giftPrice1: this.data.cashCouponText4,
    })
  },
  goDetails: function (event) {
    var newsid = event.currentTarget.dataset.newsid;
    wx.navigateTo({
      url: '../details/details?newsid=' + newsid
      // 跳转到商品详情
    })
  },
  goAddress: function () {//跳转地址页面
    wx.navigateTo({
      url: '../address/address'
    })
  },
  goCashCoupon: function () {//跳转到代金券页面
    wx.navigateTo({
      url: '../cashCoupon/cashCoupon'
    })
  }
})